#!/bin/sh
#
# Run a simple backup  then create some
#   new files, do a Differental then a bunch of query commands
#   and finally restore the two files.
#


TestName="query-test"
JobName=query
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
change_jobname CompressedTest $JobName

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list

echo "${BackupDirectory}/ficheriro1.txt" >${tmp}/restore-list
echo "${BackupDirectory}/ficheriro2.txt" >>${tmp}/restore-list


start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
label storage=File volume=TestVolume002
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=File
stop_susan

echo "ficheriro1.txt" >${BackupDirectory}/ficheriro1.txt
echo "ficheriro2.txt" >${BackupDirectory}/ficheriro2.txt

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
@# Force differental on the second Volume
update volume=TestVolume001 VolStatus=Used
run level=differential job=$JobName yes
wait
messages
END_OF_DATA

run_susan
check_for_zombie_jobs storage=File
stop_susan

echo "ficheriro2.txt" >${BackupDirectory}/ficheriro2.txt
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
run level=incremental job=$JobName yes
wait
messages
@#
@# Now do the queries
@#
query
1
ficheriro1.txt
query
2
${BackupDirectory}
ficheriro1.txt
localhost-fd
query
6
TestVolume001
query
7
1
query
8
localhost-fd
query
9
Default
query
10
query
11
query
12
1
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/susan-restores storage=File file=<${cwd}/tmp/restore-list
yes
wait
messages
quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=File
stop_susan

check_two_logs
check_restore_files_diff `cat ${cwd}/tmp/restore-list`

end_test
